<!--  -->
<template>
  <div class="basic-layout">
    <div :class="['nav-side',state.collapse?'fold':'unFold']">
      <!-- 系统logo -->
      <div class="logo">
        <img src="./../assets/logo.png">
        <span>Manager</span>
      </div>
      <el-menu :default-active="state.active" class="nav-menu" background-color="#001529" text-color="#fff" router
        :collapse="state.collapse">
        <tree-menu :userMenu="state.userMenu" />
      </el-menu>
    </div>
    <div :class="['content-right',state.collapse?'fold':'unFold']">
      <div class="nav-top">
        <div class="nav-left">
          <el-icon :size="18" class="menu-fold" @click="toggle">
            <Fold />
          </el-icon>
          <div class="bread">
            <bread-crumb></bread-crumb>
          </div>
        </div>
        <div class="user-info center">
          <el-badge :is-dot="state.notice > 0 ? true : false" class="notice" type="danger">
            <el-icon>
              <Bell />
            </el-icon>
          </el-badge>
          <el-dropdown @command="handleCommand">
            <span class="user-link">
              {{state.userInfo.userName}}
              <el-icon class="el-icon--right">
                <arrow-down />
              </el-icon>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="email">邮箱:{{state.userInfo.userEmail}}</el-dropdown-item>
                <el-dropdown-item command="logout">退出</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </div>
      <div class="wrapper">
        <router-view></router-view>
      </div>
    </div>
  </div>
</template>

<script lang='ts' setup>
import { ref, reactive, onMounted } from 'vue'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import { noticeCount, menuPermissionList, } from '@/api/index.js'
import BreadCrumb from "@/components/BreadCrumb.vue"

const store = useStore()
const router = useRouter()
// 用户信息
const state = reactive({
  collapse: false,// 菜单控制
  userInfo: store.state.userInfo,// 用户信息
  notice: 0,
  userMenu: [],
  active: location.hash.slice(1)
})

onMounted(() => {
  getNoticeCount()
  getMenuList()
  console.log(location.hash.slice(1))
})

// 切换菜单显现和隐藏
const toggle = () => {
  state.collapse = !state.collapse
}

const handleCommand = (key: string) => {
  if (key === 'email') return
  store.commit("saveUserInfo", "")
  store.commit("saveMenuList", "")

  router.push("/login")
}

const getNoticeCount = async () => {
  try {
    const res = await noticeCount()
    state.notice = res
  } catch (err) {
    console.error(err)
  }
}

const getMenuList = async () => {
  try {
    const res = await menuPermissionList()
    state.userMenu = res.menuList
    store.commit("saveMenuList", res.menuList)
    store.commit("setActionList", res.actionList)
  } catch (err) {
    console.error(err)
  }
}
</script>
<style lang='scss' scoped>
.basic-layout {
  position: relative;

  .nav-side {
    position: fixed;
    width: 200px;
    height: 100vh;
    background-color: #001529;
    color: #fff;
    transition: width .5s;
    overflow: hidden;

    .logo {
      display: flex;
      align-items: center;
      font-size: 18px;
      height: 50px;

      img {
        margin: 0 16px;
        width: 32px;
        height: 32px;
      }
    }

    .nav-menu {
      border-right: none;
      height: calc(100vh - 50px);
    }

    &.fold {
      width: 64px;
    }

    &.unfold {
      width: 200px;
    }
  }

  .content-right {
    margin-left: 200px;

    .nav-top {
      display: flex;
      justify-content: space-between;
      height: 50px;
      line-height: 50px;
      border-bottom: 1px solid #ddd;
      padding: 0 20px;

      .nav-left {
        display: flex;
        align-items: center;

        .menu-fold {
          margin-right: 15px;
        }
      }

      .user-info {
        .notice {
          display: flex;
          line-height: 30px;
          margin-right: 15px;
        }

        .user-link {
          cursor: pointer;
          color: #409eff;
        }
      }
    }

    .wrapper {
      background-color: #eef0f3;
      padding: 20px;
      height: calc(100vh - 50px);
    }

    &.fold {
      margin-left: 64px;
    }

    &.unfold {
      margin-left: 200px;
    }
  }
}
</style>